*************************************************************************************************************
*                                                                                                           *
* Do Welfare States Have Lower Carbon Emissions? The Importance of State Capacity in Lower-Income Countries * 
*                                                                                                           *
* Thomas Bernauer, Tobias Böhmelt, and Hugh Ward                                                            *
*                                                                                                           *
* Address Correspondance to: tbohmelt@essex.ac.uk                                                           *
*                                                                                                           *
* This Version: March 25, 2025                                                                              *
*                                                                                                           *
*************************************************************************************************************

graph set window fontface "Times New Roman"
use "qog_std_ts_jan24_stata14.dta", clear
xtset ccode year

gen ln_co2pc=ln(wdi_co2)
gen ln_pop=ln(wdi_pop)
gen gdp_pc=ln(wdi_gdpcappppcon2017)
gen ln_spr=ln(wdi_spr)

************************
* Descriptive Overview *
************************

preserve
qui xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, fe cluster(ccode)
keep if e(sample)
xtserial ln_co2pc wbgi_gee ln_spr p_polity2 gdp_pc ln_pop dr_ig, output
restore

qui: xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, fe cluster(ccode)
sum ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig if e(sample)
collin wbgi_gee ln_spr p_polity2 gdp_pc ln_pop dr_ig if e(sample)

***************
* Main Models *
***************

eststo clear
qui xtreg ln_co2pc l.ln_co2pc ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, fe cluster(ccode)
eststo: xtreg ln_co2pc l.ln_co2pc ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year if e(sample), fe cluster(ccode)
estimates store reg1

eststo: xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr i.year if e(sample), fe cluster(ccode)
estimates store reg2

eststo: xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, fe cluster(ccode)
estimates store reg3

eststo: xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, re cluster(ccode)
estimates store reg4

eststo: reg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, cluster(ccode)
estimates store reg5

eststo: reg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig, cluster(ccode)
estimates store reg6

esttab reg1 reg2 reg3 reg4 reg5 reg6, se star(* 0.10 ** 0.05 *** 0.01) b(%10.3f) label drop(*.year)

***********************
* Substantive Effects *
***********************

preserve
xtset ccode year
tab year, gen(year_fe)
tab ccode, gen(country_fe)
gen interaction=wbgi_gee*ln_spr
gen gdppc2=gdp_pc*gdp_pc
estsimp reg ln_co2pc l.ln_co2pc wbgi_gee ln_spr interaction p_polity2 gdp_pc gdppc2 ln_pop dr_ig year_fe* country_fe*
sum b4, detail
local x1 `r(mean)'
kdensity b4, xli(`x1', lcolor(gs10) lpattern(dash)) legend(off) scheme(538w) aspectratio(1) title("") xtitle("Simulated Interaction Effect") legend(off) note("") name(graph1, replace)
restore

preserve
generate where = -1
generate pipe = "|"
xtset ccode year
tab year, gen(year_fe)
tab ccode, gen(country_fe)
gen gdppc2=gdp_pc*gdp_pc
reg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 gdp_pc gdppc2 ln_pop dr_ig year_fe* country_fe*, cluster(ccode)
qui margins, dydx(ln_spr) at(wbgi_gee=(-2.5(0.5)1)) atmeans
marginsplot, yline(0) recast(line) ciopts(recast(rline) lpattern(dash)) level(95) name(graph2, replace) addplot (scatter where wbgi_gee if e(sample), xlabel(-2.25(0.25)0.75) ms(none) blcolor(gray) mlabel(pipe) mlabpos(0)) legend(off) scheme(538w) aspectratio(1)
restore

*********************
* Robustness Checks *
*********************

preserve
qui xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, fe cluster(ccode)
keep if e(sample)
xtile quart = wbgi_gee, nq(4)
gen outcome=ln_co2pc
twoway (lowess outcome ln_spr if quart==1) (lfit outcome ln_spr if quart==1), legend(off) scheme(plotplain) name(graph1, replace) aspectratio(1)
twoway (lowess outcome ln_spr if quart==2) (lfit outcome ln_spr if quart==2), legend(off) scheme(plotplain) name(graph2, replace) aspectratio(1)
twoway (lowess outcome ln_spr if quart==3) (lfit outcome ln_spr if quart==3), legend(off) scheme(plotplain) name(graph3, replace) aspectratio(1)
twoway (lowess outcome ln_spr if quart==4) (lfit outcome ln_spr if quart==4), legend(off) scheme(plotplain) name(graph4, replace) aspectratio(1)
graph combine graph1 graph2 graph3 graph4, ycommon
generate where = -1.5
generate pipe = "|"
xtset ccode year
tab year, gen(year_fe)
tab ccode, gen(country_fe)
gen gdppc2=gdp_pc*gdp_pc
reg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr##c.ln_spr##c.ln_spr p_polity2 gdp_pc gdppc2 ln_pop dr_ig year_fe* country_fe*, cluster(ccode)
qui margins, dydx(ln_spr) at(wbgi_gee=(-2.5(0.5)1)) atmeans
marginsplot, yline(0) recast(line) ciopts(recast(rline) lpattern(dash)) level(84) name(graphA1, replace) addplot (scatter where wbgi_gee if e(sample), xlabel(-2.5(0.5)1) ms(none) blcolor(gray) mlabel(pipe) mlabpos(0)) legend(off) scheme(538w) aspectratio(1)
restore

preserve
eststo clear
eststo: xtpcse ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.ccode, c(ar1) pairwise
estimates store regA2
eststo: reg d.ln_co2pc l.ln_co2pc c.d.wbgi_gee##c.d.ln_spr c.wbgi_gee##c.ln_spr d.p_polity2 p_polity2 c.d.gdp_pc##c.d.gdp_pc c.gdp_pc##c.gdp_pc d.ln_pop ln_pop d.dr_ig dr_ig i.ccode i.year
estimates store regA3
xtset ccode year
tab year, gen(year_fe)
tab ccode, gen(country_fe)
gen interaction=wbgi_gee*ln_spr
gen gdppc2=gdp_pc*gdp_pc
eststo: xtabond2 ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 gdp_pc gdppc2 ln_pop dr_ig year_fe* country_fe*, gmm(L1.(ln_co2pc)) iv(wbgi_gee ln_spr p_polity2 gdp_pc gdppc2 ln_pop dr_ig year_fe* country_fe*) robust
estimates store regA4
esttab regA2 regA3 regA4, se star(* 0.10 ** 0.05 *** 0.01) b(%10.3f) label drop(*.year *.ccode)
restore

preserve
gen demo_dummy=1 if p_polity2>5 & p_polity2!=.
replace demo_dummy=0 if demo_dummy==. & p_polity2!=.
gen ano_dummy=1 if p_polity2>-6 & p_polity2<6 & p_polity2!=.
replace ano_dummy=0 if ano_dummy==. & p_polity2!=.
eststo clear
eststo: xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr demo_dummy ano_dummy c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, fe cluster(ccode)
estimates store regA5
esttab regA5, se star(* 0.10 ** 0.05 *** 0.01) b(%10.3f) label drop(*.year)
restore

preserve
gen ghg_pc=ln(en_atm_ghgt_kt_ce/wdi_pop)
eststo clear
eststo: xtreg ghg_pc l.ghg_pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, fe cluster(ccode)
estimates store regA6
esttab regA6, se star(* 0.10 ** 0.05 *** 0.01) b(%10.3f) label drop(*.year)
restore

preserve
eststo clear
eststo: xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr##i.right_government p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, fe cluster(ccode)
estimates store regA7
esttab regA7, se star(* 0.10 ** 0.05 *** 0.01) b(%10.3f) label drop(*.year)
restore

preserve
eststo clear
eststo: xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.region1 i.year, re cluster(ccode)
estimates store regA8
esttab regA8, se star(* 0.10 ** 0.05 *** 0.01) b(%10.3f) label drop(*.year)
restore

preserve
collin wbgi_gee ln_spr
gen ln_spending=ln(gfs_sp)
reg ln_spending wdi_spr
predict yhat
pwcorr ln_spending wbgi_gee ln_spr, sig
eststo clear
eststo: xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.yhat p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_i i.year, fe cluster(ccode)
estimates store regA9
esttab regA9, se star(* 0.10 ** 0.05 *** 0.01) b(%10.3f) label drop(*.year)
restore

preserve
eststo clear
eststo: xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig wdi_gdpagr wdi_gdpind i.year, re cluster(ccode)
estimates store regA10
esttab regA10, se star(* 0.10 ** 0.05 *** 0.01) b(%10.3f) label drop(*.year)
restore

preserve
reg Capacity wbgi_gee
predict yhat
eststo clear
eststo: xtreg ln_co2pc l.ln_co2pc c.yhat##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, re cluster(ccode)
estimates store regA11
esttab regA11, se star(* 0.10 ** 0.05 *** 0.01) b(%10.3f) label drop(*.year)
restore

eststo clear
eststo: xtpcse ln_co2pc c.wbgi_gee##c.econ_manage p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig, c(ar1) pairwise
estimates store regA12
eststo: xtpcse ln_co2pc c.wbgi_gee##c.structural_policy p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig, c(ar1) pairwise
estimates store regA13
eststo: xtpcse ln_co2pc c.wbgi_gee##c.social_inclusion p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig, c(ar1) pairwise
estimates store regA14
eststo: xtpcse ln_co2pc c.wbgi_gee##c.pub_sector_manage p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig, c(ar1) pairwise
estimates store regA15
esttab regA12 regA13 regA14 regA15, se star(* 0.10 ** 0.05 *** 0.01) b(%10.3f) label

preserve
eststo clear
gen lagged_wbgi_gee=l.wbgi_gee
replace lagged_wbgi_gee=wbgi_gee if lagged_wbgi_gee==. & wbgi_gee!=.
gen lagged_ln_spr=l.ln_spr
replace lagged_ln_spr=ln_spr if lagged_ln_spr==. & ln_spr!=.
generate where = -2
generate pipe = "|"
xtset ccode year
tab year, gen(year_fe)
tab ccode, gen(country_fe)
qui xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_sp p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, fe cluster(ccode)
eststo: xtreg ln_co2pc c.lagged_wbgi_gee##c.lagged_ln_spr l.p_polity2 c.l.gdp_pc##c.l.gdp_pc l.ln_pop l.dr_ig i.year if e(sample), fe cluster(ccode)
reg ln_co2pc c.lagged_wbgi_gee##c.lagged_ln_spr l.p_polity2 c.l.gdp_pc##c.l.gdp_pc l.ln_pop l.dr_ig year_fe* country_fe*  if e(sample), cluster(ccode)
qui margins, dydx(lagged_ln_spr) at(lagged_wbgi_gee=(-2.5(0.5)1)) atmeans
marginsplot, yline(0) recast(line) ciopts(recast(rline) lpattern(dash)) level(84) name(graph5, replace) addplot (scatter where lagged_wbgi_gee if e(sample), xlabel(-2.25(0.25)0.75) ms(none) blcolor(gray) mlabel(pipe) mlabpos(0)) legend(off) scheme(538w) aspectratio(1)
restore

preserve
qui xtreg ln_co2pc l.ln_co2pc c.wbgi_gee##c.ln_spr p_polity2 c.gdp_pc##c.gdp_pc ln_pop dr_ig i.year, re cluster(ccode)
keep if e(sample)
keep ccode cname year
egen year_min=min(year), by(ccode)
egen year_max=max(year), by(ccode)
collapse year_min year_max, by(ccode cname)
restore